package com.hy.service.impl;

import cn.hutool.Hutool;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hy.bean.Card;
import com.hy.bean.Follow;
import com.hy.bean.User;
import com.hy.dao.UserDao;
import com.hy.service.UserService;
import com.hy.utils.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import javax.websocket.Session;
import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDao();

    @Override
    public void selectUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
        List<User> users = userDao.selectUser(username);
        ResponseResult<List<User>> result = new ResponseResult<>(200,"数据",users,101);
        ResultUtils.result(result,request,response);
    }

    @Override
    public void updateUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        int id = Integer.parseInt(request.getParameter("id"));
        String username = request.getParameter("username");
        String sex = request.getParameter("sex");
        String introduce = request.getParameter("introduce");
        String residence = request.getParameter("residence");
        String vocation = request.getParameter("vocation");
        String professionalHistory = request.getParameter("professionalHistory");
        String educationExperience = request.getParameter("educationExperience");
        String individualResume = request.getParameter("individualResume");

        Part img = null;

        if (StrUtil.isBlank(username) && StrUtil.isBlank(sex) && StrUtil.isBlank(introduce) && StrUtil.isBlank(residence) && StrUtil.isBlank(vocation) && StrUtil.isBlank(professionalHistory) && StrUtil.isBlank(educationExperience) && StrUtil.isBlank(individualResume)){
            img = request.getPart("file");
        }

        User user = userDao.selecUserById(id);

        if (!StrUtil.isBlank(username)){
            user.setUserName(username);
            HttpSession session = request.getSession(false);
            session.setAttribute("username", username);
        }else if (!StrUtil.isBlank(sex)){
            user.setSex(sex);
        }else if (!StrUtil.isBlank(introduce)){
            user.setIntroduce(introduce);
        }else if (!StrUtil.isBlank(residence)){
            user.setResidence(residence);
        }else if (!StrUtil.isBlank(vocation)){
            user.setVocation(vocation);
        }else if (!StrUtil.isBlank(professionalHistory)){
            user.setProfessionalHistory(professionalHistory);
        }else if (!StrUtil.isBlank(educationExperience)){
            user.setEducationExperience(educationExperience);
        }else if (!StrUtil.isBlank(individualResume)){
            user.setIndividualResume(individualResume);
        }else if (img != null){
            String fileUpload = FileUtils.FileUpload(img, "/zhihu1/imgs/upload", request);
            user.setImg(fileUpload);
        }

        user.setUpdateTime(DateUtil.datetime());
        user.setUpdateId(String.valueOf(id));

        boolean b = userDao.updateUser(user);
        ResponseResult<User> result = new ResponseResult<>(200,"数据",userDao.selecUserById(id),101);
        ResultUtils.result(result,request,response);
    }

    @Override
    public void countInterest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
        User user = userDao.selectById(username);
//        我的关注
        Long myInterest = userDao.selectMyInterest(user.getId());
//        关注我的人数
        Long myNoticer = userDao.selectMyNoticer(user.getId());

        HashMap<Object, Object> map = new HashMap<>();
        map.put("user", user);
        map.put("interestCount",myInterest);
        map.put("noticerCount",myNoticer);
        ArrayList<HashMap<Object, Object>> arrayList = new ArrayList<>();
        arrayList.add(map);
        ResponseResult<ArrayList<HashMap<Object, Object>>> result = new ResponseResult<>(200, "ok", arrayList, 101);
        ResultUtils.result(result,request,response);
    }

    @Override
    public void listMyInterestUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
//        获取当前登录用户的id
        User user = userDao.selectById(username);
//        当前登录用户关注的人
        List<Map<String, Object>> selectMyInterestUser = userDao.selectMyInterestUser(user.getId());

        for (Map<String, Object> map : selectMyInterestUser) {
            Integer id = (Integer) map.get("id");
            map.put("cardCount",userDao.selectCardByUserId(id));
            map.put("interestCount", userDao.selectMyNoticer(id));
        }

        ResponseResult<List<Map<String, Object>>> result = new ResponseResult<>(200, "ok", selectMyInterestUser, 101);
        ResultUtils.result(result,request,response);
    }

    @Override
    public void listFollowMeUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
        User user = userDao.selectById(username);

        List<Map<String, Object>> mapList = userDao.selectFollowMeUser(user.getId());

        for (Map<String, Object> map : mapList) {
            Integer id = (Integer) map.get("id");
            map.put("interestCount", userDao.selectMyNoticer(id));
            boolean flag = userDao.selectWhetherInterest(user.getId(), id);
            if (flag){
                map.put("mutualAttention","相互关注");
                map.put("btn","已关注");
            }else {
                map.put("mutualAttention","关注了你");
                map.put("btn","+  关注");
            }
        }
        ResponseResult<List<Map<String, Object>>> result = new ResponseResult<>(200, "ok", mapList, 101);
        ResultUtils.result(result,request,response);
    }

    @Override
    public void updateFollowTombstone(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String userId = request.getParameter("userId");
        String user1Id = request.getParameter("user1Id");
        String tombstone = request.getParameter("tombstone");

        Follow follow = new Follow();
        follow.setUserId(Integer.valueOf(userId));
        follow.setUser1Id(Integer.valueOf(user1Id));
        follow.setUpdateTime(DateUtil.datetime());
        follow.setUpdateId(Integer.valueOf(userId));
        follow.setTombstone(tombstone);

        boolean b = userDao.updateFollowTombstone(follow);

        if (b){
            ResponseResult<Boolean> result = new ResponseResult<>(200, "ok", b, 101);
            ResultUtils.result(result,request,response);
        }else {
            ResponseResult<Boolean> result = new ResponseResult<>(200, "ok", b, 102);
            ResultUtils.result(result,request,response);
        }
    }

    @Override
    public void saveFollow(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String userId = request.getParameter("userId");
        String user1Id = request.getParameter("user1Id");
        boolean flag = false;
        flag = userDao.selectWhetherInterest1(Integer.valueOf(userId), Integer.valueOf(user1Id));
        System.out.println(flag);
        Follow follow = new Follow();
        if (flag){
            follow.setUserId(Integer.valueOf(userId));
            follow.setUser1Id(Integer.valueOf(user1Id));
            follow.setUpdateTime(DateUtil.datetime());
            follow.setUpdateId(Integer.valueOf(userId));
            follow.setTombstone("0");
            flag = userDao.updateFollowTombstone(follow);
        }else {
            follow.setUserId(Integer.valueOf(userId));
            follow.setUser1Id(Integer.valueOf(user1Id));
            follow.setCreateTime(DateUtil.datetime());
            follow.setCreateId(Integer.valueOf(userId));
            follow.setTombstone("0");
            flag = userDao.insertFollow(follow);
        }

        ResponseResult<Boolean> result = new ResponseResult<>(200, "ok", flag, 101);
        ResultUtils.result(result,request,response);
    }

    @Override
    public void mutualOrNotAttention(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
        String username1 = request.getParameter("username1");

        User user = userDao.selectById(username);
        User user1 = userDao.selectById(username1);

        boolean b = userDao.selectWhetherInterest(user.getId(), user1.getId());

        ResponseResult<Boolean> result = new ResponseResult<>(200, "ok", b, 101);
        ResultUtils.result(result,request,response);
    }

}
